package com.gitee.elasticsearch.common.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 开发人员   YooLin1c
 * 编写日期   2018/6/17
 * 联系邮箱   401664157@qq.com
 */
@Configuration
public class ElasticSearchConfig {
    @Bean
    public TransportClient transportClient() throws UnknownHostException {
        //注意：这里使用ES的TCP端口9300而不是HTTP端口9200
        InetSocketTransportAddress masterNode = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300);
        InetSocketTransportAddress slave1Node = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9301);
        InetSocketTransportAddress slave2Node = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9302);
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch_cluster").build();
        TransportClient transportClient = new PreBuiltTransportClient(settings);
        transportClient.addTransportAddress(masterNode);
        transportClient.addTransportAddress(slave1Node);
        transportClient.addTransportAddress(slave2Node);
        return transportClient;
    }
}
